package persentation.presentUniversityDean;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


public class MyTabbedPane extends JTabbedPane {

	private ArrayList<String> contents;
	private ImageIcon CLOSE=new ImageIcon("Graphics/shut2.png");
	private ImageIcon TOUCH=new ImageIcon("Graphics/shut1.png");
	private BottomPanel bottomPanel;
	
	public MyTabbedPane(){
		contents=new ArrayList<>();
		
	}
	
	private boolean hasContent(String title){
		for(String string:contents){
			if (title.equals(string)) {
				return true;
			}
			
		}
		return false;
	}
	
	public void addOne(String title,final JComponent component){

		if (hasContent(title)) {
			setSelectedComponent(component);
		}
		else {
			
			JPanel tab=new JPanel();
			tab.setOpaque(false);
			JLabel tabLabel=new JLabel(title);
			final JLabel close=new JLabel(CLOSE);
			tab.add(tabLabel,BorderLayout.WEST);
			tab.add(close,BorderLayout.EAST);
			close.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					super.mouseClicked(e);
					
				//	int closerTabNumber = indexOfComponent(content);
				//	System.out.println(closerTabNumber);
					int index=indexOfComponent(component);
					System.out.println(index);
					removeTabAt(index);
					removeOne(index);
					
				}

				@Override
				public void mouseEntered(MouseEvent e) {
					super.mouseMoved(e);
					close.setIcon(TOUCH);
					
				}

				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					super.mouseExited(e);
					close.setIcon(CLOSE);
				}
			});
			
			addTab(title,component);
			setSelectedComponent(component);
			setTabComponentAt(getSelectedIndex(), tab);
			
			component.repaint();
			contents.add(title);
		}
	}
	
	public void removeOne(int index){
		System.out.println(contents.get(index));
		contents.remove(index);
		if (contents.isEmpty()) {
			addOne("欢迎使用", bottomPanel);
		}
		
	}
	
	public void addBottom(BottomPanel bottomPanel){
		this.bottomPanel=bottomPanel;
	}
}
